package org.jeecg.modules.smartcampus.com.smartcampus.controller;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.json.JSONObject;
import org.hibernate.annotations.GenericGenerator;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.smartcampus.UUIDTest;
import org.jeecg.modules.smartcampus.com.smartcampus.entity.Signin;
import org.jeecg.modules.smartcampus.com.smartcampus.entity.SigninStart;
import org.jeecg.modules.smartcampus.com.smartcampus.mapper.SigninMapper;
import org.jeecg.modules.smartcampus.com.smartcampus.mapper.SigninStartMapper;
import org.jeecg.modules.smartcampus.com.smartcampus.service.ISigninService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 签到信息表
 * @Author: jeecg-boot
 * @Date:   2020-08-07
 * @Version: V1.0
 */
@Api(tags="签到信息表")
@RestController
@RequestMapping("/com.smartcampus/signin")
@Slf4j
public class SigninController extends JeecgController<Signin, ISigninService> {
	@Autowired
	private ISigninService signinService;
	@Autowired
	private SigninMapper signinMapper;
	@Autowired
	private SigninStartMapper signinStartMapper;
	 /*@TableId(type = IdType.ID_WORKER_STR)
	 @ApiModelProperty(value = "主键ID")
	 private java.lang.String id;*/
	 //private String idOut;
	 //@Id
	 //@GeneratedValue(strategy = GenerationType.IDENTITY)
	 //private java.lang.String id;
	 @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
	 private java.lang.String id;
	 /**
	 * 分页列表查询
	 *
	 * @param signin
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "签到信息表-分页列表查询")
	@ApiOperation(value="签到信息表-分页列表查询", notes="签到信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Signin signin,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Signin> queryWrapper = QueryGenerator.initQueryWrapper(signin, req.getParameterMap());
		Page<Signin> page = new Page<Signin>(pageNo, pageSize);
		IPage<Signin> pageList = signinService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param signin
	 * @return
	 */
	@AutoLog(value = "签到信息表-添加")
	@ApiOperation(value="签到信息表-添加", notes="签到信息表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Signin signin) {
		signinService.save(signin);
		return Result.ok("添加成功！");
	}

	 /**
	  *   发起签到
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "签到信息表-添加")
	 @ApiOperation(value="签到信息表-添加", notes="签到信息表-添加")
	 @PostMapping(value = "/addsignin")
	 public Object addsignin(String tno, String cno) {
		 long sino=signinStartMapper.getSinNo()+1;
		 String id=UUIDTest.getGuid();
		 SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Date sitime;
		 Date nowdate = new Date();
		 Timestamp.valueOf(simpleDate.format(nowdate));
		 sitime=Timestamp.valueOf(simpleDate.format(nowdate));
		 System.out.println(".............start signin.............\n"+sitime);
		 signinStartMapper.insertSinIn(sino,tno,cno,sitime,id,sitime);

		 JSONObject json =new JSONObject();
		 json.put("result"," success");
		 json.put("sino",sino);
		 json.put("id",id);
		 //response.setCharacterEncoding("utf-8");
		 //response.setContentType("application/json;charset=utf-8");
		 //PrintWriter out = null;
		 //out = response.getWriter();
		 //out.write(json.toString());
		 return json;
	 }

	 /**
	  *   手机端签到
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "签到信息表-添加")
	 @ApiOperation(value="签到信息表-添加", notes="签到信息表-添加")
	 @PostMapping(value = "/signin")
	 public Result<?> addfromphone(String sino,String id,String tno, String cno, String sno) {
		 SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Date sitime;
		 Date nowdate = new Date();
		 Timestamp.valueOf(simpleDate.format(nowdate));
		sitime=Timestamp.valueOf(simpleDate.format(nowdate));
		 System.out.println(".............signin.............\n"+sitime);
	 	signinMapper.insertSinIn(sino,tno,cno,sno,sitime,id,sitime);
		 return Result.ok("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param signin
	 * @return
	 */
	@AutoLog(value = "签到信息表-编辑")
	@ApiOperation(value="签到信息表-编辑", notes="签到信息表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Signin signin) {
		signinService.updateById(signin);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "签到信息表-通过id删除")
	@ApiOperation(value="签到信息表-通过id删除", notes="签到信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		signinService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "签到信息表-批量删除")
	@ApiOperation(value="签到信息表-批量删除", notes="签到信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.signinService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "签到信息表-通过id查询")
	@ApiOperation(value="签到信息表-通过id查询", notes="签到信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Signin signin = signinService.getById(id);
		if(signin==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(signin);
	}

	 /**
	  * 查询当前sino
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "签到信息表-通过id查询")
	 @ApiOperation(value="签到信息表-通过id查询", notes="签到信息表-通过id查询")
	 @GetMapping(value = "/SearchSino")
	 public Result<?> SearchSino(@RequestParam(name="id",required=true) String id) {
		 Signin signin = signinService.getById(id);
		 if(signin==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.ok(signin);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param signin
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Signin signin) {
        return super.exportXls(request, signin, Signin.class, "签到信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Signin.class);
    }

}
